package com.Alan.service.supplier;

import com.Alan.dao.BaseDao;
import com.Alan.dao.supplier.SupplierDao;
import com.Alan.dao.supplier.SupplierDaoImpl;
import com.Alan.pojo.Supplier;
import org.junit.Test;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class SupplierServiceImpl implements SupplierService{

    SupplierDao supplierDao = null;

    public SupplierServiceImpl(){
        supplierDao = new SupplierDaoImpl();
    }

    @Override
    public int getSupCount(String supName, String contacts) {
        Connection connection = BaseDao.getConnection();
        int supCount = supplierDao.getSupCount(connection, supName, contacts);

        BaseDao.closeResource(connection, null, null);
        return supCount;
    }

    @Override
    public List<Supplier> getSupList(String supName, String contacts, int startRow, int pageRows) {
        Connection connection = BaseDao.getConnection();
        List<Supplier> supList = supplierDao.getSupList(connection, supName, contacts, startRow, pageRows);

        BaseDao.closeResource(connection, null, null);
        return supList;
    }

    @Override
    public boolean supExist(String supCode) {
        Connection connection = BaseDao.getConnection();
        boolean flag = false;

        if (supplierDao.supExist(connection, supCode) > 0){
            flag = true;
        }

        BaseDao.closeResource(connection, null, null);

        return flag;
    }

    @Override
    public Supplier getSupInformation(int supId) {
        Connection connection = BaseDao.getConnection();
        Supplier supplier = supplierDao.getSupInformation(connection, supId);

        BaseDao.closeResource(connection, null, null);
        return supplier;
    }

    @Override
    public boolean updateSup(Supplier supplier) {
        Connection connection = BaseDao.getConnection();
        boolean flag = false;

        if (supplierDao.updateSup(connection, supplier) > 0){
            flag = true;
        }

        BaseDao.closeResource(connection, null, null);
        return flag;
    }

    @Override
    public boolean addSup(Supplier supplier) {
        Connection connection = BaseDao.getConnection();
        boolean flag = false;

        if (supplierDao.addSup(connection, supplier) > 0){
            flag = true;
        }

        BaseDao.closeResource(connection, null, null);
        return flag;
    }

    @Override
    public boolean deleteSup(int supId) {
        Connection connection = BaseDao.getConnection();
        boolean flag = false;

        if (supplierDao.deleteSup(connection, supId) > 0){
            flag = true;
        }

        return flag;
    }

    @Override
    public List<Supplier> getSupNames() {
        Connection connection = BaseDao.getConnection();
        List<Supplier> supNames = supplierDao.getSupNames(connection);
        BaseDao.closeResource(connection, null, null);

        return supNames;
    }

    @Test
    public void test(){
        SupplierService supplierService = new SupplierServiceImpl();

        /*System.out.println(supplierService.getSupCount("", ""));*/

        /*List<Supplier> supList = supplierService.getSupList("", "", 0, 2);
        for (Supplier supplier : supList) {
            System.out.println(supplier.toString());
        }*/

        /*Supplier supInformation = supplierService.getSupInformation(1);
        System.out.println(supInformation.toString());*/

        System.out.println(supplierService.supExist("tt"));
    }
}
